//
//  yiYuanDuoBaoViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/13.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
//屏幕宽度 是375

#import "yiYuanDuoBaoViewController.h"
#import "Macros.h"
#import "HttpRequest.h"
#import "homeViewController.h"
#import "UIImageView+WebCache.h"
#import "renQiTuiJian.h"
#import "customCollectionViewCell.h"
#import "yiYuanDuoBao.h"
#import "zuiXinJieXiaoViewController.h"
#import "goodsDetailViewController.h"
#import "yiYuanDuoBaoNextView.h"
#import "fenLeiController.h"
#import "shaiDanViewController.h"
#import "shaiDan.h"
#import "TCLYUser.h"

#import "SVProgressHUD.h"

#import "goodsDetail.h"

@interface yiYuanDuoBaoViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIScrollView *backScrollView;

@property (weak, nonatomic) IBOutlet UIView *backGroundView;
@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property(nonatomic,retain)UIPageControl*upc;
//最新揭晓文字
@property (weak, nonatomic) IBOutlet UILabel *label;
//分类
@property (weak, nonatomic) IBOutlet UILabel *fenLei;
//晒单
@property (weak, nonatomic) IBOutlet UILabel *shaiDanLabel;
@property (weak, nonatomic) IBOutlet UIView *next;
@property (weak, nonatomic) IBOutlet UILabel *renQiTuiJianLabel;
//获取轮播图图片的数组
@property(nonatomic,copy)NSMutableArray *picArray;
//人气推荐图片的数组
@property(nonatomic,copy)NSMutableArray *jsonArray;
//一元夺宝列表
@property(nonatomic,copy)NSMutableArray *indiaArray;

@property(nonatomic,copy)NSString *picString;
//轮播图的图片
@property(nonatomic,retain)UIImageView *imgView01;
@property(nonatomic,retain)UIImageView *imgView02;
@property(nonatomic,retain)UIImageView *imgView03;
@property(nonatomic,copy)NSMutableArray *numArray;
//装人气推荐的背景view
@property(nonatomic,retain)UIView *pic_View;
@property(nonatomic,retain)UICollectionView *collectionView;
//collectionView的高度
@property(nonatomic,assign)float collectionView_height;

@end

@implementation yiYuanDuoBaoViewController
@synthesize imageScrollView,upc,picArray,jsonArray,imgView01,imgView02,imgView03,numArray,pic_View,indiaArray;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    picArray=[NSMutableArray array];
    jsonArray=[NSMutableArray array];
    numArray=[NSMutableArray array];
    indiaArray=[NSMutableArray array];
    //设置背景色
    [self setBackGroundColor];
    [self createHeaderScrollCiew];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    [self getDataFromNet];
    shaiDan *sd=[shaiDan shardeManager];
    if (sd.whichViewToBack==100)
    {
        sd.whichViewToBack=100;
    }else{
        sd.whichViewToBack=1;
    }
    goodsDetail *gd=[goodsDetail shardeManager];
    gd.num=100;
}
//设置背景色
-(void)setBackGroundColor
{
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.label.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
    self.fenLei.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
    self.shaiDanLabel.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
    self.next.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    self.renQiTuiJianLabel.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
    self.backGroundView.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
}
//创建轮播图
-(void)createHeaderScrollCiew
{
    CGSize size=CGSizeMake(screenWidth*3, 0);
    imageScrollView.contentSize=size;
    imgView01=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 125)];
    imgView02=[[UIImageView alloc]initWithFrame:CGRectMake(screenWidth, 0, screenWidth, 125)];
    imgView03=[[UIImageView alloc]initWithFrame:CGRectMake(screenWidth*2, 0, screenWidth, 125)];
    imgView01.image=[UIImage imageNamed:@"banner1"];
    imgView02.image=[UIImage imageNamed:@"banner1"];
    imgView03.image=[UIImage imageNamed:@"banner1"];
    [imageScrollView addSubview:imgView01];
    [imageScrollView addSubview:imgView02];
    [imageScrollView addSubview:imgView03];
    imageScrollView.pagingEnabled=YES;
    imageScrollView.delegate=self;
    //scroll.backgroundColor=[UIColor blueColor];
    imageScrollView.showsHorizontalScrollIndicator=NO;
    imageScrollView.showsVerticalScrollIndicator=NO;
    upc=[[UIPageControl alloc]initWithFrame:CGRectMake((screenWidth-0.4*screenWidth)/2+0.16*screenWidth, 80, 0.4*screenWidth, 66)];
    upc.numberOfPages=3;
    upc.currentPage=0;
    [self.backGroundView addSubview:upc];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    upc.currentPage=scrollView.contentOffset.x/screenWidth;
}
//从服务器获取数据
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/indiana/ida_list"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"uid"]=userID;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //整个字典
         NSDictionary*json_data=[json objectForKey:@"data"];
         //获取轮播图的图片
         picArray=[json_data objectForKey:@"carousel"];
         //获取人气推荐的图片
         jsonArray=[json_data objectForKey:@"recommend"];
         //一元夺宝列表
         indiaArray=[json_data objectForKey:@"india"];
         if (picArray.count>0&&jsonArray.count>0&&indiaArray.count>0)
         {
//             [self setImageInPicArray];
//             //获得人气图片
//             [self getRenQiTuiJianImage];
//             [self setCollectionViewPic];
             [self dismiss];
         }
//         NSLog(@"json_data=%@",json_data);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    [SVProgressHUD dismiss];
    [self setImageInPicArray];
    //获得人气图片
    [self getRenQiTuiJianImage];
    [self setCollectionViewPic];
}

//从网上获取轮播图的图片
-(void)setImageInPicArray
{
    for (int i=0; i<picArray.count; i++)
    {
        NSString *str=[picArray[i] objectForKey:@"pic"];
        NSURL* imagePath = [NSURL URLWithString:str];
        if (i==0)
        {
            //给一张默认图片，先使用默认图片，当图片加载完成后再替换
            [imgView01 sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"banner1"]];
        }
        else if (i==1)
        {
            [imgView02 sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"banner1"]];
        }else if (i==2)
        {
            [imgView03 sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"banner1"]];
        }
    }
}
//获取人气推荐的图片
-(void)getRenQiTuiJianImage
{
    float pic_Width=screenWidth/4-2;
    float pic_Height=205;
    float pic_y=self.next.frame.size.height+self.next.frame.origin.y;
    for (int i=0; i<4; i++)
    {
        //
        renQiTuiJian *rqtj=[renQiTuiJian shardeManager];
        rqtj.content=[jsonArray[i] objectForKey:@"content"];
        rqtj.idString=[jsonArray[i] objectForKey:@"id"];
        rqtj.invole=[jsonArray[i] objectForKey:@"invole"];
        rqtj.name=[jsonArray[i] objectForKey:@"name"];
        rqtj.pic=[jsonArray[i] objectForKey:@"pic"];
        rqtj.sum=[jsonArray[i] objectForKey:@"sum"];
        
        pic_View=[[UIView alloc]initWithFrame:CGRectMake(pic_Width*i+2*i, pic_y, pic_Width, pic_Height)];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0.01333*screenWidth, 0.01333*screenWidth, pic_Width-0.02667*screenWidth, 42)];
        label.numberOfLines=0;
        label.textColor=[UIColor blackColor];
        label.font=[UIFont systemFontOfSize:15.0];
        label.textAlignment=NSTextAlignmentCenter;
        label.text=rqtj.name;
        float imageView_y=label.frame.size.height+label.frame.origin.y;
        float imageWidth=pic_Width-0.02667*screenWidth;
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0.01333*screenWidth, imageView_y,imageWidth,imageWidth)];
        NSURL* imagePath = [NSURL URLWithString:rqtj.pic];
        [imageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
        float grayView_y=imageView.frame.origin.y+imageView.frame.size.height+5;
        UIView*grayView=[[UIView alloc]initWithFrame:CGRectMake(0.01333*screenWidth, grayView_y, imageWidth, 4)];
        grayView.backgroundColor=[UIColor grayColor];
        float involve=[rqtj.invole intValue];
        float sum=[rqtj.sum intValue];
        float i_sum=involve/sum;
//        NSLog(@"i_sum=%f",i_sum);
        UIView*redView=[[UIView alloc]initWithFrame:CGRectMake(0.01333*screenWidth, grayView_y, imageWidth*i_sum, 4)];
        redView.backgroundColor=[UIColor redColor];
        pic_View.backgroundColor=[UIColor whiteColor];
        UILabel *invole_label=[[UILabel alloc]initWithFrame:CGRectMake(0.01333*screenWidth, grayView_y+8,60, 21)];
        invole_label.text=rqtj.invole;
        invole_label.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
        UILabel *sum_label=[[UILabel alloc]initWithFrame:CGRectMake(imageWidth-20, grayView_y+8, 60, 21)];
        sum_label.text=rqtj.sum;
        sum_label.textColor=[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0];
        float commitBtn_y=sum_label.frame.size.height+sum_label.frame.origin.y+5;
        UIButton *commitBtn=[[UIButton alloc]initWithFrame:CGRectMake(0.01333*screenWidth, commitBtn_y, imageWidth-5, 30)];
        int tag=[rqtj.idString intValue];
        [numArray addObject:rqtj.idString];
        commitBtn.tag=tag;
        [commitBtn setTitle:@"参与" forState:UIControlStateNormal];
        [commitBtn setTitleColor:[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0] forState:UIControlStateNormal];
        [commitBtn addTarget:self action:@selector(joinIn:) forControlEvents:UIControlEventTouchUpInside];
        //设置按钮的圆角半径不会被遮挡
        [commitBtn.layer setMasksToBounds:YES];
//        [commitBtn.layer setCornerRadius:10];
        //设置边界的宽度
        [commitBtn.layer setBorderWidth:1];
        [commitBtn.layer setBorderColor:[UIColor colorWithRed:233/255.0 green:30/255.0 blue:99/255.0 alpha:1.0].CGColor];
        [pic_View addSubview:commitBtn];
        [pic_View addSubview:sum_label];
        [pic_View addSubview:invole_label];
        [pic_View addSubview:grayView];
        [pic_View addSubview:redView];
        [pic_View addSubview:imageView];
        [pic_View addSubview:label];
        [self.backGroundView addSubview:pic_View];
    }
}
//设置一元夺宝列表的collectionView
-(void)setCollectionViewPic
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(screenWidth/2-2,0.7733*screenWidth/2);
//    flowLayout.sectionInset = UIEdgeInsetsMake(2, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    float collectionView_y=pic_View.frame.origin.y+pic_View.frame.size.height+3;
//    NSLog(@"indiaArray.count=%ld",indiaArray.count);
    if (indiaArray.count%2==0)
    {
        self.collectionView_height=(indiaArray.count/2)*0.7733*screenWidth/2+10;
    }else if(indiaArray.count%2!=0)
    {
        int a=(int)indiaArray.count/2+1;
        self.collectionView_height=a*0.7733*screenWidth/2+10;
    }
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, collectionView_y, screenWidth, self.collectionView_height) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //注册
    [self.collectionView registerClass:[customCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.backGroundView addSubview:self.collectionView];
    self.backGroundView.frame=CGRectMake(self.backGroundView.frame.origin.x, self.backGroundView.frame.origin.y, self.backGroundView.frame.size.width,collectionView_y+self.collectionView_height);
    //设置backScrollView的size
    CGSize size=CGSizeMake(0, collectionView_y+self.collectionView_height);
    self.backScrollView.contentSize=size;
}
//设定指定区内Cell的最小行距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
{
    return 5;
}
//设定指定区内Cell的最小间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
{
    return 2;
}
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return indiaArray.count;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"cell";
    customCollectionViewCell *cell = (customCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    yydb.content=[indiaArray[indexPath.row] objectForKey:@"content"];
    yydb.idString=[indiaArray[indexPath.row] objectForKey:@"id"];
    yydb.invole=[indiaArray[indexPath.row] objectForKey:@"invole"];
    yydb.name=[indiaArray[indexPath.row] objectForKey:@"name"];
    yydb.pic=[indiaArray[indexPath.row] objectForKey:@"pic"];
    yydb.sum=[indiaArray[indexPath.row] objectForKey:@"sum"];
    cell.contentLabel.text=yydb.content;
    cell.involeLabel.text=yydb.invole;
    cell.sumLabel.text=yydb.sum;
    float sum=[yydb.sum intValue];
    float involve=[yydb.invole intValue];
    
//    sum/involve*cell.grayView.frame.size.width;
    cell.redView.frame=CGRectMake(cell.redView.frame.origin.x, cell.redView.frame.origin.y, involve/sum*cell.grayView.frame.size.width, 3);
//    NSLog(@"cell.sumLabel.text=%@",yydb.sum);
    cell.nameLabel.text=yydb.name;
    NSURL* imagePath = [NSURL URLWithString:yydb.pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [cell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    [cell.joinBtn addTarget:self action:@selector(joinTo:) forControlEvents:UIControlEventTouchUpInside];
    int num=[yydb.idString intValue];
    cell.joinBtn.tag=num;
    return cell;
}
//图片列表
-(void)joinTo:(UIButton*)sender
{
    yiYuanDuoBao *yydb=[yiYuanDuoBao shardeManager];
    yydb.idString=[NSString stringWithFormat:@"%ld",sender.tag];
    yiYuanDuoBaoNextView *yydbn=[yiYuanDuoBaoNextView shardeManager];
    yydbn.whichViewToNext=1;
    goodsDetailViewController *gdvc=[goodsDetailViewController new];
    [self.navigationController pushViewController:gdvc animated:YES];
}
//人气推荐的按钮点击方法
-(void)joinIn:(UIButton*)sender
{
    yiYuanDuoBaoNextView *yydbn=[yiYuanDuoBaoNextView shardeManager];
    yiYuanDuoBao*yydb=[yiYuanDuoBao shardeManager];
    yydbn.whichViewToNext=1;
    int num0=[numArray[0] intValue];
    int num1=[numArray[1] intValue];
    int num2=[numArray[2] intValue];
    int num3=[numArray[3] intValue];
    
    if (sender.tag==num0)
    {
        yydb.idString=numArray[0];
        goodsDetailViewController *gdvc=[goodsDetailViewController new];
        [self.navigationController pushViewController:gdvc animated:YES];
    }else if (sender.tag==num1)
    {
        yydb.idString=numArray[1];
        goodsDetailViewController *gdvc=[goodsDetailViewController new];
        [self.navigationController pushViewController:gdvc animated:YES];
    }else if (sender.tag==num2)
    {
        yydb.idString=numArray[2];
        goodsDetailViewController *gdvc=[goodsDetailViewController new];
        [self.navigationController pushViewController:gdvc animated:YES];
    }else if (sender.tag==num3)
    {
        yydb.idString=numArray[3];
        goodsDetailViewController *gdvc=[goodsDetailViewController new];
        [self.navigationController pushViewController:gdvc animated:YES];
    }
}
//最新揭晓点击的按钮方法
- (IBAction)zuiXinJieXiao:(id)sender
{
    zuiXinJieXiaoViewController *zxjx=[zuiXinJieXiaoViewController new];
    [self.navigationController pushViewController:zxjx animated:YES];
}
//分类点击的按钮方法
- (IBAction)fenLei:(id)sender
{
    fenLeiController *flc=[fenLeiController new];
    [self.navigationController pushViewController:flc animated:YES];
}
//晒单点击的按钮方法
- (IBAction)shaiDan:(id)sender
{
    shaiDan *sd=[shaiDan shardeManager];
    sd.whichViewToBack=3;
    shaiDanViewController*sdvc=[shaiDanViewController new];
    [self.navigationController pushViewController:sdvc animated:YES];
}
- (IBAction)goBack:(id)sender
{
//    homeViewController *hvc=[homeViewController new];
    shaiDan *sd=[shaiDan shardeManager];
    int num=sd.whichViewToBack;
//    NSLog(@"num=%d",num);
    if (num==1)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[shaiDanViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
